package com.baimifan.renovation.publicfrag;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.baimifan.renovation.R;
import com.baimifan.renovation.adapter.DemoCardAdapter;
import com.baimifan.renovation.manager.DemoFragManager;

import java.lang.ref.WeakReference;

public class DemoFragment extends Fragment {
    private Float w, h, widthPixels, heightPixels;
    private int typeId;
    private RecyclerView recyclerView;
    private DemoCardAdapter demoCardAdapter;
    private DemoFragManager demoFragManager;
    public static DemoFragment newInstance (int typeId) {
        DemoFragment demoFragment = new DemoFragment();
        Bundle args = new Bundle();
        args.putInt("typeId", typeId);
        demoFragment.setArguments(args);
        return demoFragment;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            if (getArguments() != null) {
                typeId = getArguments().getInt("typeId", 1);
                demoFragManager = new DemoFragManager(new MyHandler(this));
                demoFragManager.getDemo(typeId+"");
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_demo, container, false);
        init(view);
        getDisplay();
        setLayoutParams();
        return view;
    }

    private void init (View view ) {
        recyclerView = (RecyclerView) view.findViewById(R.id.demo_recyclerview);
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
    }

    private void setLayoutParams () {
        LinearLayout.LayoutParams params_r = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        params_r.setMargins((int)(w*4), 0, 0,0);
        recyclerView.setLayoutParams(params_r);
    }

    private void getDisplay() {
        DisplayMetrics metrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
        widthPixels = Float.valueOf(metrics.widthPixels);
        heightPixels = Float.valueOf(metrics.heightPixels);
        w = widthPixels / 360;
        h = heightPixels / 640;
    }

    private class MyHandler extends Handler {
        WeakReference<DemoFragment> mdemo;

        public MyHandler (DemoFragment demoFragment) {
            mdemo = new WeakReference<DemoFragment>(demoFragment);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            DemoFragment demo = mdemo.get();
            switch (msg.what) {
                case 11 :
                    demoCardAdapter = new DemoCardAdapter(demoFragManager.getDemos(), w, h);
                    recyclerView.setAdapter(demoCardAdapter);
                    demoCardAdapter.notifyDataSetChanged();
                    break;
            }
        }
    }

}
